package com.example.netty.wechat;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class AppleGroupService {
    private static final List<Group>  groups= new ArrayList<>();
    private static long groupNo= 10000;

    public static Group newGroup(String groupName,Request request){
        Group group= new Group(groupName);
        group.addUsers(new User(request.getRequestId(), request.getName()));
        groups.add(group);
        return group;
    }

    public static List<User> findUsersByGroup(String groupId){
        for(Group group:groups){
            if(groupId.equals(group.groupId)){
                return group.getUsers();
            }
        }
        return new ArrayList<>();
    }

    public static Group joinGroup(String groupId,Request request){
       for (Group group:groups){
           if(group.groupId.equals(groupId)){
               group.addUsers(new User(request.getRequestId(),request.getName()));
               return group;
           }
       }return null;
    }

    public static List<String> findGroups(String groupName){
        List<Group> ss= groups.stream().filter(group -> group.groupName.contains(groupName)).collect(Collectors.toList());
        return  ss.stream().map(new Function<Group, String>() {
            @Override public String apply(Group group) {
                return group.groupName;
            }
        }).collect(Collectors.toList());

    }

    public static boolean  groupUniqueCheck(String groupId){
        for (Group group:groups){
            if(group.groupId.equals(groupId)){
                return true;
            }
        }
        return false;
    }

    public static long genGroupNo(){
        return groupNo++;
    }
}
